from django.db import models
from utils.models import CoreModel


# Create your models here.


class Users(models.Model):
    username = models.CharField(max_length=50, unique=True, db_index=True, verbose_name='用户账号',
                                help_text="用户账号")
    password = models.CharField(null=True, max_length=255, verbose_name="密码", help_text="密码")
    nickname = models.CharField(max_length=100, help_text="用户昵称", verbose_name="用户昵称", default="")
    IDENTITY_CHOICES = (
        (1, "超级管理员"),
        (2, "普通管理员"),
    )
    identity = models.SmallIntegerField(choices=IDENTITY_CHOICES, verbose_name="身份标识", null=True, blank=True,
                                        default=2, help_text="身份标识")
    # 状态
    STATUS_CHOICES = (
        (1, "正常"),
        (2, "禁用"),
    )
    avatar = models.CharField(max_length=255, verbose_name="用户头像", help_text="用户头像", default='')
    mobile = models.CharField(max_length=255, verbose_name="电话", null=True, blank=True, help_text="电话")
    gender = models.CharField(default="男", verbose_name="性别", null=True, blank=True, max_length=1,
                              help_text="性别")
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name="状态：1-正常 2-禁用",
                                 help_text="状态：1-正常 2-禁用")
    post = models.CharField(verbose_name='关联岗位', null=True, blank=True, max_length=255,
                            help_text="关联岗位")
    role = models.CharField(verbose_name='关联角色', null=True, blank=True, max_length=255,
                            help_text="关联角色")
    dept = models.CharField(verbose_name='所属部门', max_length=255, null=True, blank=True, help_text="关联部门")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建用户时间",
                                       help_text="创建用户时间"),
    update_time = models.DateTimeField(auto_now=True, verbose_name="用户更新时间",
                                       help_text="用户更新时间")

    class Meta:
        # 数据表名
        db_table = "system_user"
        verbose_name = "后台用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "用户{}".format(self.id)


class Token(models.Model):
    userId = models.IntegerField(default=1, verbose_name="用户ID",
                                 help_text="用户ID")
    token = models.CharField(max_length=255, help_text="用户登录token", verbose_name="用户登录token", default="",
                             null=True)

    class Meta:
        # 数据表名
        db_table = "system_cache_token"
        verbose_name = "用户缓存表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "用户{}".format(self.id)


class Menu(CoreModel):
    parent = models.ForeignKey(to='Menu', on_delete=models.CASCADE, verbose_name="上级菜单", null=True, blank=True,
                               db_constraint=False, help_text="上级菜单")
    icon = models.CharField(max_length=64, default='ant-design:book-outlined', verbose_name="菜单图标",
                            help_text="菜单图标")
    title = models.CharField(max_length=64, verbose_name="菜单名称", help_text="菜单名称")
    permission = models.CharField(max_length=64, null=True, blank=True, verbose_name="权限标识", help_text="权限标识")
    ISLINK_CHOICES = (
        (0, "否"),
        (1, "是"),
    )
    is_ext = models.BooleanField(default=False, verbose_name="是否外链", help_text="是否外链")

    TYPE_CHOICES = (
        (0, "目录"),
        (1, "菜单"),
    )
    type = models.IntegerField(choices=TYPE_CHOICES, verbose_name="是否目录", help_text="是否目录")
    path = models.CharField(max_length=128, verbose_name="路由地址", null=True, blank=True, help_text="路由地址")
    redirect = models.CharField(max_length=128, verbose_name="重定向地址", null=True, blank=True,
                                help_text="重定向地址")

    component = models.CharField(max_length=128, verbose_name="组件地址", null=True, blank=True, help_text="组件地址")
    name = models.CharField(max_length=50, verbose_name="组件名称", null=True, blank=True, help_text="组件名称")
    status = models.BooleanField(default=True, blank=True, verbose_name="菜单状态", help_text="菜单状态")
    keepalive = models.BooleanField(default=False, blank=True, verbose_name="是否页面缓存", help_text="是否页面缓存")
    hide_menu = models.BooleanField(default=False, blank=True, verbose_name="侧边栏中是否隐藏",
                                    help_text="侧边栏中是否隐藏")

    class Meta:
        db_table = "system_menu"
        verbose_name = '菜单表'
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class LoginLog(CoreModel):
    LOGIN_TYPE_CHOICES = (
        (1, '普通登录'),
    )
    username = models.CharField(max_length=32, verbose_name="登录用户名", null=True, blank=True, help_text="登录用户名")
    ip = models.CharField(max_length=32, verbose_name="登录ip", null=True, blank=True, help_text="登录ip")
    agent = models.TextField(verbose_name="agent信息", null=True, blank=True, help_text="agent信息")
    browser = models.CharField(max_length=200, verbose_name="浏览器名", null=True, blank=True, help_text="浏览器名")
    os = models.CharField(max_length=200, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    continent = models.CharField(max_length=50, verbose_name="州", null=True, blank=True, help_text="州")
    country = models.CharField(max_length=50, verbose_name="国家", null=True, blank=True, help_text="国家")
    province = models.CharField(max_length=50, verbose_name="省份", null=True, blank=True, help_text="省份")
    city = models.CharField(max_length=50, verbose_name="城市", null=True, blank=True, help_text="城市")
    district = models.CharField(max_length=50, verbose_name="县区", null=True, blank=True, help_text="县区")
    isp = models.CharField(max_length=50, verbose_name="运营商", null=True, blank=True, help_text="运营商")
    area_code = models.CharField(max_length=50, verbose_name="区域代码", null=True, blank=True, help_text="区域代码")
    country_english = models.CharField(max_length=50, verbose_name="英文全称", null=True, blank=True,
                                       help_text="英文全称")
    country_code = models.CharField(max_length=50, verbose_name="简称", null=True, blank=True, help_text="简称")
    longitude = models.CharField(max_length=50, verbose_name="经度", null=True, blank=True, help_text="经度")
    latitude = models.CharField(max_length=50, verbose_name="纬度", null=True, blank=True, help_text="纬度")
    login_type = models.IntegerField(default=1, choices=LOGIN_TYPE_CHOICES, verbose_name="登录类型",
                                     help_text="登录类型")

    class Meta:
        db_table = 'system_login_log'
        verbose_name = '登录日志'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)
